﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace BAL
{
    public class StorageWrapper
    {
        private static string NORMAL_HOST = System.Configuration.ConfigurationManager.AppSettings["StorageDomain"];
        private static string THUMB_HOST = System.Configuration.ConfigurationManager.AppSettings["StorageDomain"];
        private static string NORMAL_HOST_GAME = System.Configuration.ConfigurationManager.AppSettings["StorageDomain"];
        private static string THUMB_HOST_GAME = System.Configuration.ConfigurationManager.AppSettings["StorageDomain"];

        private string _secretkey = System.Configuration.ConfigurationManager.AppSettings["SecretKey"];
        public string SecretKey
        {
            get { return _secretkey; }
            set { _secretkey = value; }
        }

        private string _action = System.Configuration.ConfigurationManager.AppSettings["LargeFile"];
        private string Action
        {
            get { return _action; }
            set { _action = value; }
        }

        private Dictionary<string, string> _mimes;
        public Dictionary<string, string> MIMES
        {
            get { return _mimes; }
            set { _mimes = value; }
        }

        public StorageWrapper(string SecretKey)
        {
            this.SecretKey = SecretKey;
            FillMimes();
        }

        public StorageWrapper()
        {
            FillMimes();
        }

        private void FillMimes()
        {
            if (_mimes == null) _mimes = new Dictionary<string, string>();
            _mimes.Add(".jpeg", "image/jpeg");
            _mimes.Add(".jpg", "image/jpeg");
            _mimes.Add(".JPG", "image/jpeg");
            _mimes.Add(".JPEG", "image/jpeg");
            _mimes.Add(".gif", "image/gif");
            _mimes.Add(".png", "image/png");
            _mimes.Add(".PNG", "image/png");
        }

        public string ViewDirectory(string Dir)
        {
            string viewaction = _action + "ls?dirname=" + Dir + "&secret_key=" + SecretKey + "&timestamp=" + DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
            HttpWebResponse resp = (HttpWebResponse)HttpMethodHelper.HttpGet(viewaction, null);
            StreamReader reader = new StreamReader(resp.GetResponseStream());
            string result = reader.ReadToEnd();
            reader.Close();
            resp.Close();
            return result;
        }

        private string GetMime(string FileName)
        {
            if (FileName.Contains("."))
            {
                string ext = FileName.Substring(FileName.LastIndexOf("."), FileName.Length - FileName.LastIndexOf("."));
                if (_mimes.ContainsKey(ext)) return _mimes[ext];
                else return String.Empty;
            }
            else return String.Empty;
        }

        public bool Upload(string FileName, string FilePath, bool OverWrite)
        {
            string uploadaction = _action + "upload";
            NameValueCollection parameters = new NameValueCollection();
            parameters.Add("filename", FileName);
            parameters.Add("secret_key", _secretkey);
            if (OverWrite) parameters.Add("overwrite", "1");
            HttpWebResponse resp = (HttpWebResponse)HttpMethodHelper.PostFile(new Uri(uploadaction), parameters, FilePath, GetMime(FileName), "filedata", null, null);
            bool result = false;
            if (resp.StatusCode == HttpStatusCode.OK) result = true;
            resp.Close();
            return result;
        }

        public bool Upload(string FileName, Stream FileData, bool OverWrite)
        {
            string uploadaction = _action + "upload";
            NameValueCollection parameters = new NameValueCollection();
            parameters.Add("filename", FileName);
            parameters.Add("secret_key", _secretkey);
            if (OverWrite) parameters.Add("overwrite", "1");
            HttpWebResponse resp = (HttpWebResponse)HttpMethodHelper.PostFile(new Uri(uploadaction), parameters, FileData, FileName, GetMime(FileName), "filedata", null, null);
            bool result = false;
            if (resp.StatusCode == HttpStatusCode.OK) result = true;
            resp.Close();
            return result;
        }

        public bool MakeDir(string DirName)
        {
            string mkdiraction = _action + "make_dir?secret_key=" + SecretKey + "&dirname=" + DirName;
            HttpWebResponse resp = (HttpWebResponse)HttpMethodHelper.HttpGet(mkdiraction, null);
            bool result = false;
            if (resp.StatusCode == HttpStatusCode.OK) result = true;
            resp.Close();
            return result;
        }

        public bool RemoveDir(string DirName)
        {
            string rmaction = _action + "remove_dir?secret_key=" + SecretKey + "&dirname=" + DirName;
            HttpWebResponse resp = (HttpWebResponse)HttpMethodHelper.HttpGet(rmaction, null);
            bool result = false;
            if (resp.StatusCode == HttpStatusCode.OK) result = true;
            resp.Close();
            return result;
        }

        public bool Delete(string FileName)
        {
            string dlaction = _action + "delete?secret_key=" + SecretKey + "&filename=" + FileName.Trim('/');
            HttpWebResponse resp = (HttpWebResponse)HttpMethodHelper.HttpGet(dlaction, null);
            bool result = false;
            if (resp.StatusCode == HttpStatusCode.OK) result = true;
            resp.Close();
            return result;
        }

        public bool Rename(string From, string To)
        {
            string rnaction = _action + "delete?secret_key=" + SecretKey + "&from=" + From + "&to=" + To;
            HttpWebResponse resp = (HttpWebResponse)HttpMethodHelper.HttpGet(rnaction, null);
            bool result = false;
            if (resp.StatusCode == HttpStatusCode.OK) result = true;
            resp.Close();
            return result;
        }

        public static string Thumb_In(int Width, int Height, string FileName)
        {
            return THUMB_HOST + "/thumb_in/" + Width + "_" + Height + (FileName.StartsWith("/") ? "" : "/") + FileName;
        }

        public static string Thumb(int Width, int Height, string FileName)
        {
            return THUMB_HOST + "/thumb/" + Width + "_" + Height + (FileName.StartsWith("/") ? "" : "/") + FileName;
        }

        public static string Thumb_W(int Width, string FileName, bool isDomain = true)
        {
            return isDomain ? THUMB_HOST + "/thumb_w/" + Width + (FileName.StartsWith("/") ? "" : "/") + FileName
                : "/thumb_w/" + Width + (FileName.StartsWith("/") ? "" : "/") + FileName;
        }

        public static string Thumb_WL(int Width, string FileName)
        {
            return THUMB_HOST + "/thumb_wl/" + Width + (FileName.StartsWith("/") ? "" : "/") + FileName;
        }

        public static string Thumb_R(int Width, float Ratio, string FileName)
        {
            return THUMB_HOST + "/thumb_r/" + Width + "_" + Ratio + (FileName.StartsWith("/") ? "" : "/") + FileName;
        }

        public static string Crop(int Width, int Height, string FileName)
        {
            return THUMB_HOST + "/crop/" + Width + "_" + Height + (FileName.StartsWith("/") ? "" : "/") + FileName;
        }

        public static string Zoom(int Width, int Height, string FileName)
        {
            return THUMB_HOST + "/zoom/" + Width + "_" + Height + (FileName.StartsWith("/") ? "" : "/") + FileName;
        }

        public static string Original(string FileName)
        {
            return NORMAL_HOST + (FileName.StartsWith("/") ? "" : "/") + FileName;
        }
        //---GAME
        public static string Thumb_InGAME(int Width, int Height, string FileName)
        {
            return THUMB_HOST_GAME + "/thumb_in/" + Width + "_" + Height + (FileName.StartsWith("/") ? "" : "/") + FileName;
        }

        public static string ThumbGAME(int Width, int Height, string FileName)
        {
            return THUMB_HOST_GAME + "/thumb/" + Width + "_" + Height + (FileName.StartsWith("/") ? "" : "/") + FileName;
        }

        public static string Thumb_WGAME(int Width, string FileName)
        {
            return THUMB_HOST_GAME + "/thumb_w/" + Width + (FileName.StartsWith("/") ? "" : "/") + FileName;
        }

        public static string Thumb_WLGAME(int Width, string FileName)
        {
            return THUMB_HOST_GAME + "/thumb_wl/" + Width + (FileName.StartsWith("/") ? "" : "/") + FileName;
        }

        public static string Thumb_RGAME(int Width, float Ratio, string FileName)
        {
            return THUMB_HOST_GAME + "/thumb_r/" + Width + "_" + Ratio + (FileName.StartsWith("/") ? "" : "/") + FileName;
        }

        public static string CropGAME(int Width, int Height, string FileName)
        {
            return THUMB_HOST_GAME + "/crop/" + Width + "_" + Height + (FileName.StartsWith("/") ? "" : "/") + FileName;
        }

        public static string ZoomGAME(int Width, int Height, string FileName)
        {
            return THUMB_HOST_GAME + "/zoom/" + Width + "_" + Height + (FileName.StartsWith("/") ? "" : "/") + FileName;
        }

        public static string OriginalGAME(string FileName)
        {
            return THUMB_HOST_GAME + (FileName.StartsWith("/") ? "" : "/") + FileName;
        }
    }
}
